package com.lusang.org.service.impl;

import com.lusang.basics.service.impl.BaseServiceImpl;
import com.lusang.org.domain.Department;
import com.lusang.org.mapper.DepartmentMapper;
import com.lusang.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    //继承了基础事务的实现类，同时还实现了自己的部门事务接口
    @Autowired
    DepartmentMapper mapper;
    /*重写新增和修改 自动生成创建赫尔修改时间*/
    @Override
    public void modify(Department department) {
        department.setUpdate_time(new Date());//获取更新的时间
        //获取path
        Long deptId = department.getId();
        String path = null;
        if(department.getParent()==null||department.getParent().getId()==null){//判断是否是顶级部门
            path = "/"+deptId;
        }else {
            Long pid = department.getParent().getId();
            path = super.getOne(pid).getPath() + "/" +deptId;
        }
        department.setPath(path);
        super.modify(department);
    }

    @Override
    public void add(Department department) {
        department.setCreate_time(new Date());//获取创建时间

        super.add(department);//保存后就返回自增主键id
        //获取path
        Long deptId = department.getId();
        String path = null;
        if(department.getParent()==null||department.getParent().getId()==null){//判断是否是顶级部门
            path = "/"+deptId;
        }else {
            Long pid = department.getParent().getId();
            path = super.getOne(pid).getPath() + "/" +deptId;
        }
        department.setPath(path);
        super.modify(department);
    }

    @Override
    public List<Department> getTree() {
        return mapper.loadTree();
    }
}
